package cn.semes.basic.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextUtil implements ApplicationContextAware {

    private static ApplicationContext context;

    // springboot加载完成后会把beanfactory作为参数传给次方法，然后我们可以把工厂赋值给context。
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
    // 通过context获取bean
    public static <T> T getBean(Class<T> name) {
        return context.getBean(name);
    }
}
